<?php

namespace AdminModule;

class VyrobciPresenter extends AdminPresenter {

    public function actionDefault() {
        $manufacturerModel = new Models\Manufacturer($this->db);
        $this->template->manufacturers = $manufacturerModel->order('title');
    }
    
    public function actionPridat() {
        
    }
    
    public function actionUpravit($id) {
        $manufacturerModel = new Models\Manufacturer($this->db);
        $manufacturer = $manufacturerModel->get($id);
        
        if (!$manufacturer) {
            $this->flashMessage('Výrobce se zadaným id neexistuje', 'error');
            $this->redirect('default');
        }
    }
    
    public function handleSmazat($id) {
        $manufacturerModel = new Models\Manufacturer($this->db);
        $manufacturer = $manufacturerModel->get($id);
        
        if ($manufacturer && $manufacturer->delete()) {
            $this->flashMessage('Výrobce byl odstraněn');
        } else {
            $this->flashMessage('Výrobce se nepodařilo odstranit');
        }
        
        $this->invalidateControl('content');
        
        if (!$this->isAjax()) {
            $this->redirect('this');
        }
    }
    
    protected function createComponentManufacturersForm() {
        $form = new \Nette\Application\UI\Form;
        
        $form->addText('title', 'Název')
                ->setRequired('Zadejte název výrobce');
        $form->addText('discount', 'Sleva v %')
                ->addCondition(\Nette\Application\UI\Form::FILLED)                
                ->addRule(\Nette\Application\UI\Form::INTEGER, 'Sleva musí být celé číslo');
        
        if ($this->action == 'upravit') {            
            $manufacturerModel = new Models\Manufacturer($this->db);
            $manufacturer = $manufacturerModel->get($this->getParameter('id'));
            
            $form->setDefaults($manufacturer);
            $form->addSubmit('add', 'Uložit');
        } else {
            $form->addSubmit('add', 'Přidat');
        }
        
        $form->onSuccess[] = $this->manufacturersFormSubmitted;
        
        return $form;
    }
    
    public function manufacturersFormSubmitted(\Nette\Application\UI\Form $form) {
        $values = $form->getValues();
        
        $manufacturerModel = new Models\Manufacturer($this->db);
        
        if ($this->action == 'pridat') {
            if ($manufacturerModel->insert($values) !== false) {
                $this->flashMessage('Výrbce byl přidán');
            } else {
                $this->flashMessage('Výrobce se nepodařilo přidat', 'error');
            }
        } else {
            $manufacturer = $manufacturerModel->get($this->getParameter('id'));
            if ($manufacturer->update($values) !== false) {
                $this->flashMessage('Výrobce byl upraven');
            } else {
                $this->flashMessage('Výrobce se nepodařilo upravit', 'error');
            }
        }
        
        $this->redirect('default');
    }

}
